package com.atguigu.controller;


import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.Roleservice;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    @DubboReference
    private Roleservice roleservice;
    @DubboReference
    private PermissionService permissionService;

    @PreAuthorize("hasAuthority('role.show')")
    @RequestMapping()
    public String index(Model model, HttpServletRequest request) {
        /*
         * 本来是查询所有角色信息进行全部展示但是需要做查询发分页进行改版
         * */
        //通过Request 对象获取map<String,Object> filters 用于存放查询的条件页码数 页大小
        Map<String, Object> filters = getFilters(request);
        //查询分页信息
        PageInfo<Role> page = roleservice.findPage(filters);
        model.addAttribute("page", page);
        //将搜索的信息放入请求域当中，进行回显到页面
        model.addAttribute("filters", filters);
        return "role/index";
    }


    //渲染角色信息输入页面
    @PreAuthorize("hasAuthority('role.create')")
    @RequestMapping("/create")
    public String create() {
        return "role/create";
    }

    //接收客户局端提交的新增role的信息，将信息保存进入数据库
    //渲染成功页面响应给客户端
    @PreAuthorize("hasAuthority('role.create')")
    @RequestMapping("/save")
    public String save(Role role) {
        //接收请求中的参数，转换成 一个role对
        //调用服务层方法，调用role的index方法
        roleservice.index(role);
        return "common/successPage";
    }

    /*
     * 接受用户的修改请求
     * 获得要修改的角色的id
     * 根据id 查询对应role的对象
     * role对象需要放入请求域
     * 渲染一个展示role信息的视图
     *
     * */
    @PreAuthorize("hasAuthority('role.edit')")
    @RequestMapping("/edit/{id}")
    public String edit(ModelMap modelMap, @PathVariable("id") Long id) {
        //根据id进行查询role的信息
        Role role = roleservice.getById(id);
        //将数据放入请求域
        modelMap.addAttribute("role", role);
        //渲染展示员工信息的视图
        return "role/edit";

    }


    /*
     * update更新role信息
     * 将role更新进入数据库
     * 渲染页面
     * */
    @PreAuthorize("hasAuthority('role.edit')")
    @RequestMapping("/update")
    public String update(Role role) {
        Integer res = roleservice.update(role);
        return "common/successPage";
    }
    /*
     * 删除角色处理器
     * 接收id
     * 删除完毕 重定向
     * */
    @PreAuthorize("hasAuthority('role.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        roleservice.delete(id);
        return "redirect:/role";
    }
    /*
    * 查询不论is_delete的所有用户
    * */
    @RequestMapping("recover")
    public String recover(ModelMap modelMap){
        List<Role> roles = roleservice.finAllisdelete();
        modelMap.addAttribute("list",roles);
        return "role/recover";
    }
    /**
     * 修改权限
     */
    @RequestMapping("/updateis/{id}")
    public String updateis(@PathVariable("id") Long id){
        roleservice.updateisdelte(id);
        return "common/successPage";
    }
    @PreAuthorize("hasAuthority('role.assgin')")
    @RequestMapping("/assignPermission")
    public String assignPermission(Long roleId,Long[] permissionIds){
        permissionService.saveRolePermissionRealtionShip(roleId,permissionIds);
        return "common/successPage";
    }

//显示权限设计图处理器
@PreAuthorize("hasAuthority('role.assgin')")
    @RequestMapping("/assginShow/{roleId}")
    public String assshow(@PathVariable("roleId") Long roleId,ModelMap modelMap){
        List<Map<String,Object>> list = permissionService.findPermissionByRoleId(roleId);
        modelMap.addAttribute("zNodes",list);
        modelMap.addAttribute("roleId",roleId);
        return "role/assignShow";
    }
}
















